<template>
  <div class="components-container">
    <el-card class="box-card" shadow="always">
      <div class="clearfix" slot="header">
        <b>
          <svg-icon icon-class="menu"/>
          菜单列表</b>
      </div>
      <!-- 搜索表单 -->
      <el-form
        ref="queryForm"
        size="mini"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item>
          <el-button type="success" icon="el-icon-plus" @click="addOrUpdateHandle()" :disabled="isAuth('sys:menu:add')">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.name"
            placeholder="菜单名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table
          highlight-current-row
          border
          size="mini"
          v-loading="loading"
          :data="menuList"
          row-key="menuId"
          @row-click="handleRowClick"
          :tree-props="{children: 'children'}">
        <el-table-column label="菜单名称">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.icon || ''"></svg-icon>&nbsp
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column
            prop="type"
            label="类型"
            width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 0" size="small" type="info" >目录</el-tag>
            <el-tag v-else-if="scope.row.type === 1" size="small"  effect="plain">菜单</el-tag>
            <el-tag v-else-if="scope.row.type === 2" size="small" type="info">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                v-if="scope.row.type === 0"
                type="success"
                icon="el-icon-plus"
                size="mini"
                circle
                plain
                @click.stop="addOrUpdateHandle(scope.row,true)"
            />
            <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
                plain
                :disabled="isAuth('sys:menu:update')"
                @click.stop="addOrUpdateHandle(scope.row)"
            />
            <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                circle
                plain
                :disabled="isAuth('sys:menu:delete')"
                @click.stop="handleDelete(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="handleQuery($event)"></add-or-update>
  </div>
</template>

<script>
import { deleteMenu,listAllMenu,deleteMenuTree } from '@/api/system/menu'
import AddOrUpdate from './menu-add-or-update'
export default {
  name: "Menu",
  components:{ AddOrUpdate },
  data() {
    return {
      menuList:[],
      addOrUpdateVisible: false,
      loading: true,
      queryParams: {
        name: undefined
      },
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle (row,flag) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row,flag)
      })
    },
    handleQuery(flag){
      listAllMenu(this.queryParams).then(res => {
        if (res.status) {
          this.menuList = res.data;
          this.loading = false
          if (flag === 'true') {
            this.$store.dispatch('getUserInfo')
          }
        }else {
          this.$message.error("获取菜单列表失败")
        }
      })
    },
    handleDelete(row){
      if (row.children.length > 0){
        this.$confirm(`是否删除[名称=${row.name}]及其下的所有菜单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMenuTree(row.menuId).then(res => {
            if (res.status) {
              this.handleQuery()
              this.$message.success("删除成功")
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(() => {});
      }else {
        this.$confirm(`确定对[名称=${row.name}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMenu(row.menuId).then(res => {
            if (res.status) {
              this.handleQuery()
              this.$message.success("删除成功")
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(() => {});
      }
    },
    handleReset() {
      this.queryParams.name = undefined
      this.handleQuery()
    },
    handleRowClick(row) {
        this.$emit('menuClick', row)
    }
  }
}
</script>

<style scoped>

</style>
